// miniprogram/pages/user/history/index/index.js
const app = getApp()
const db=wx.cloud.database()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    nowMore: '',
    selectShow: false,//控制下拉列表的显示隐藏，false隐藏、true显示
    selectData: ['全部', '大一', '大二', '大三', '大四'],//下拉列表的数据
    index: 0,//选择的下拉列表下标
    current　: 'left',
    listData: [],
    listData_left: [],
    listData_right: [],
    listData_mid: [],
    listData_end: [],
    tipHidden : true,
    idList:[],
    ev:{target:{dataset:{current:"left"}}}

  },
  // 点击下拉显示框
  selectTap() {
    this.setData({
      selectShow: !this.data.selectShow
    });
  },
  // 点击下拉列表
  optionTap(e) {
    let Index = e.currentTarget.dataset.index;//获取点击的下拉列表的下标
    this.setData({
      index: Index,
      selectShow: !this.data.selectShow
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const eventChannel = this.getOpenerEventChannel()
    var that = this
     // 监听acceptDataFromOpenerPage事件，获取上一页面通过eventChannel传送到当前页面的数据
     eventChannel.on('acceptDataFromOpenerPage', function(data) {
     // console.log(data.data.Interest) 
      that.setData({
        idList:data.data.Interest
      },()=>{
        that.getInterest()
      })
    })

 



  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  handleCurrent(ev) {
    
    let current = ev.target.dataset.current;
    if (current == this.data.current) {
      
      return false;
    }
   
    if (current == 'left') {
      this.data.listData = this.data.listData_left;
    }
    else if (current == 'mid') {
      this.data.listData = this.data.listData_mid;
    }
    else if (current == 'right') {
      this.data.listData = this.data.listData_right;

    }
    else {
      this.data.listData = this.data.listData_end;
    }
    this.setData({
      current: current,
      listData:this.data.listData
    });

  },
  handleTip:function (e){
    console.log(e)
      if (this.data.tipHidden == false) {
      this.setData({
        tipHidden: true
      })
    }
    else {
      this.setData({
        tipHidden: false,
        nowMore: e.currentTarget.dataset.topic
      })
    }
  },

  getInterest:function()
  {
      
      const idList = this.data.idList
      let i=0;
      let id="";
      let len = this.data.idList.length
      const that  =this
     
      
  
  
     for(i=0;i<len;i++)
     {
           id = idList[i]; 
           db.collection('SportActivity').doc(id).get({
           success:res=>{
                that.data.listData.push(res.data)
                that.setData({
                  listData:this.data.listData
                })
               that.data.listData_left.push(res.data)
          
           }
         })
     }
     for(i=0;i<len;i++)
     {
           id = idList[i]; 
           db.collection('AnotherAcitivity').doc(id).get({
           success:res=>{
                
               that.data.listData_end.push(res.data)
          
           }
         })
     }
     for(i=0;i<len;i++)
     {
           id = idList[i]; 
           db.collection('AcademicActivity').doc(id).get({
           success:res=>{
  
               that.data.listData_mid.push(res.data)
           
           }
         })
     }
     for(i=0;i<len;i++)
     {
           id = idList[i]; 
           db.collection('ArtActivity').doc(id).get({
           success:res=>{
               that.data.listData_right.push(res.data)
               
           }
         })
     }
   


  

  },

  fn:function()
  {
    const that  =this
    return new Promise((resolve, reject) => {
      setTimeout(function () {
        resolve(that.getInterest());//返回写函数里面你要执行的内容
      },3000)
  }
)
}

})